# from django.shortcuts import render

# Create your views here.
import json

from django.http import HttpResponse, JsonResponse


def index(request):
    print("这是第一个函数")

    return HttpResponse("NO1的首页")

# 前端传参的四种方式
# 一、查询字符串参数（get请求：浏览器验证）
def getreq(request):

    a = request.GET.get("a")
    b = request.GET.get("b")
    alist = request.GET.getlist("a")

    print(a)
    print(b)
    print(alist)

    return HttpResponse("查询字符串参数")


# 二、路径传参（get请求：浏览器验证）
def pathreq(request,city,date):

    print(city)
    print(date)

    return HttpResponse("路径传参")

# 三、表单传参（post请求：POSTMAN验证）
def freq(request):

    a = request.POST.get("a")
    b = request.POST.get("b")
    alist = request.POST.getlist("a")

    print(a)
    print(b)
    print(alist)

    return HttpResponse("表单传参")

# 四、JSON传参--默认是byte类型（post请求：POSTMAN验证）

def jsonreq(request):

    dict = json.loads(request.body.decode())

    print(dict.get("a"))
    print(dict.get("b"))

    return HttpResponse("JSON传参")

# 请求头

def req(request):

    print(request.META['CONTENT_TYPE'])

    return HttpResponse("请求头")

# 响应

def demo_res(request):

    str = '{"name":"wangwu","age":30}'

    return HttpResponse(str,content_type='application/json',status=200)

# 响应的特殊用法
def res_t(request):

    response = HttpResponse("itcast  django",status=200)

    response['itcast'] = 'django'

    return response

# JsonResponse类

def jsonres(request):

    dict = {'city':'xian','weathe':'sunny'}

    return JsonResponse(dict)